//
//  PASelfInfo.m
//  PAMobileWallet
//
//  Created by Gavin Zhang on 7/16/13.
//  Copyright (c) 2013 平安付. All rights reserved.
//

#import "PASelfInfo.h"
#import "PATimeLineInfo.h"
#import "PABankCardInfo.h"
@implementation PASelfInfo

@synthesize baseInfo;
@synthesize balance;
@synthesize availableBalance;
@synthesize idCardNo;
@synthesize loginPassword;
@synthesize bankCardInfos           = _bankCardInfos;
@synthesize timeLineInfos           = _timeLineInfos;
- (id)init
{
    self = [super init];
    if (self) {
        self.bankCardInfos = [[NSMutableArray alloc] initWithCapacity:0];
        self.timeLineInfos = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return self;
}
- (void)loadLoginInfoDic:(NSDictionary *)dic
{
    [self updateBaseInfo:[dic objectForKey:k_userInfo]];
    self.balance = [[dic objectForKey:k_balance] doubleValue];
    self.availableBalance = [[dic objectForKey:K_availableBalance] doubleValue];
    self.idCardNo = [dic objectForKey:k_idCardNo];
    [self updateBankCardInfoDics:[dic objectForKey:k_bankCardInfos]];
    [self updateTimeLineInfoDics:[dic objectForKey:k_timeLineInfos]];
}
- (void)loadFromCache
{
    [self loadTimeLineInfoFromCache];
}

- (void)updateTimeLineInfoDics:(NSArray *)timeLineInfoDics
{
    NSMutableArray *timeLineInfos = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSDictionary *dic in timeLineInfoDics) {
        PATimeLineInfo *timeLineInfo = [[PATimeLineInfo alloc] initWithDic:dic];
        [timeLineInfos addObject:timeLineInfo];
    }
    self.timeLineInfos = timeLineInfos;
}
- (void)updateTimeLineInfoCacheDics:(NSArray *)timeLineInfoDics
{
    NSString *fileName = [NSString stringWithFormat:@"timeLineInfoDics_%@",self.baseInfo.account];
    NSString *filePath = [[DocumentsPath stringByAppendingPathComponent:fileName] stringByAppendingPathExtension:@"plist"];
    [timeLineInfoDics writeToFile:filePath atomically:YES];
}
- (void)loadTimeLineInfoFromCache
{
    NSString *fileName = [NSString stringWithFormat:@"timeLineInfoDics_%@",self.baseInfo.account];
    NSString *filePath = [[DocumentsPath stringByAppendingPathComponent:fileName] stringByAppendingPathExtension:@"plist"];
    NSArray *timeLineInfoDics = [NSArray arrayWithContentsOfFile:filePath];
    [self updateTimeLineInfoDics:timeLineInfoDics];
}
- (void)updateBankCardInfoDics:(NSArray *)bankCardInfoDics
{
    NSMutableArray *bankCardInfos = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSDictionary *dic in bankCardInfoDics) {
        PABankCardInfo *bankCardInfo = [[PABankCardInfo alloc] initWithDic:dic];
        [bankCardInfos addObject:bankCardInfo];
    }
    self.bankCardInfos = bankCardInfos;
}
- (void)updateBankCardInfoCacheDics:(NSArray *)bankCardInfoDics
{
    NSString *filePath = [[DocumentsPath stringByAppendingPathComponent:@"loginInfo"] stringByAppendingPathExtension:@"plist"];
    NSDictionary *cacheLoginInfo = [NSDictionary dictionaryWithContentsOfFile:filePath];
    NSMutableDictionary *cacheDic = [[NSMutableDictionary alloc] initWithDictionary:cacheLoginInfo];
    [cacheDic setObject:bankCardInfoDics forKey:k_bankCardInfos];
    [cacheDic writeToFile:filePath atomically:YES];
}
- (void)updateBaseInfo:(NSDictionary *)userInfoDic
{
    self.baseInfo = [[PAUserInfo alloc] initWithDic:userInfoDic];
}
@end
